/*
 * @Author: 晟松
 * @Date: 2021-12-10 16:07:50
 * @LastEditors: 晟松
 * @LastEditTime: 2021-12-10 17:24:52
 * @FilePath: \mini-vue3d:\Code\node\koa2\volunteer-service\src\main\middleware\routeMid\validateIdentity.js
 * @Description: 路由级别验证身份的中间件,依赖于jwt的ctx.state.user信息，判断传来的token是属于什么身份
 */
import { IdentityError } from "@main/common/CommonError";
import enums from "@main/config/enums";
/**
 * @description:
 * @param {*} identity 身份值，如student，shcool
 * @return {*}
 */

export async function allowSchool(ctx, next) {
    try {
        let { identity } = ctx.state.user;
        if (identity != "school") {
            throw new IdentityError("allowSchool");
        }
    } catch (error) {
        throw error;
    }

    await next();
}
export async function allowStudent(ctx, next) {
    try {
        let { identity } = ctx.state.user;
        if (identity != "student") {
            throw new IdentityError("allowStudent");
        }
    } catch (error) {
        throw error;
    }

    await next();
}
export async function allowResident(ctx, next) {
    try {
        let { identity } = ctx.state.user;
        if (identity != "resident") {
            throw new IdentityError("allowResident");
        }
    } catch (error) {
        throw error;
    }

    await next();
}
export async function allowCommunity(ctx, next) {
    try {
        let { identity } = ctx.state.user;
        if (identity != "community") {
            throw new IdentityError("allowCommunity");
        }
    } catch (error) {
        throw error;
    }

    await next();
}

export async function isSchoolAdmin(ctx, next) {
    try {
        // 多重解构
        let {
            identity,
            school: { managementLevel },
        } = ctx.state.user;

        if (!(identity == "school" && managementLevel == enums.managementLevel['school'])) {
            throw new IdentityError("allowCommunity");
        }
    } catch (error) {
        throw error;
    }

    await next();
}

/**
 * @description:
 * @param {*} identity 被允许的身份
 * @return {*}
 */
export default function allowUserIdentity(allowIdentity) {
    return async function allow(ctx, next) {
        try {
            let { identity } = ctx.state.user;
            if (Array.isArray(allowIdentity)) {
                if (!allowIdentity.includes(identity)) {
                    throw new IdentityError(
                        "not allow identity: " +
                            identity +
                            "\nallow: " +
                            allowIdentity
                    );
                }
            } else {
                if (allowIdentity != identity) {
                    throw new IdentityError(
                        "not allow identity: " +
                            identity +
                            "\nallow: " +
                            allowIdentity
                    );
                }
            }
        } catch (error) {
            throw error;
        }

        await next();
    };
}
